layer = 1;
exp=3;
timepoints = length(proj_meta(exp).ExpGroup);

for i=1:4
    cell_no(i)=length(proj_meta(exp).rd(i,1).ROIinfo);
end

% %M07
% nframes(1,1:15000)=1:15000;
% nframes(2,1:15000)=1:15000;
% nframes(3,1:15000)=[1:5000,10001:20000];
% nframes(4,1:15000)=[1:5000,10001:20000];
% nframes(5,1:15000)=[1:5000,10001:15000,20001:25000];
%M02_2
nframes(1,1:15000)=[1:5000,10001:15000,20001:25000];
nframes(2,1:15000)=[1:5000,10001:20000];
nframes(3,1:15000)=[1:10000,15001:20000];
nframes(4,1:15000)=[1:15000];
% %M02_1
% nframes(1,1:15000)=[1:5000,15001:20000,20001:25000];
% nframes(2,1:15000)=1:15000;
% nframes(3,1:15000)=[1:15000];
% nframes(4,1:15000)=[1:15000];
%%
tmp=zeros(sum(cell_no),15000);
for ind=1:timepoints
    figure;
    subplot(4,1,1)
    tmp2(1:10000) = proj_meta(exp).rd(layer,ind).velP_smoothed(nframes(ind,1:10000));
    tmp2(10001:15000)=ones(5000,1)*-0.005;
    plot(tmp2,'r')
    hold on
    plot(proj_meta(exp).rd(layer,ind).velM_smoothed(nframes(ind,:))-0.05,'k')
    axis tight
    axis off
    subplot(4,1,2:4)
    for jnd=1:4
        tmp(sum(cell_no(1:jnd-1))+1:sum(cell_no(1:jnd)),:) = ...
            proj_meta(exp).rd(jnd,ind).act(:,nframes(ind,:));
    end
    imagesc(tmp);
    set(gca,'XTick',0:5000:15000)
    set(gca,'Clim',[1 4])
end
%%
clear fb pb dark fb2 pb2 dark2
for ind=1:timepoints
    clear visual running ac
    visual = proj_meta(exp).rd(layer,ind).velP_smoothed(nframes(ind,:));
    running = proj_meta(exp).rd(layer,ind).velM_smoothed(nframes(ind,:));
    ac = proj_meta(exp).rd(layer,ind).act(:,nframes(ind,:));
    for i = 1:size(ac,1)
        fb(ind,i)=corr2(running(1:5000),smooth2(ac(i,1:5000),10));
        pb(ind,i)=corr2(running(5001:10000),smooth2(ac(i,5001:10000),10));
        dark(ind,i)=corr2(running(10001:15000),smooth2(ac(i,10001:15000),10));
    end
    fb2(ind) = corr2(running(1:5000),mean(ac(:,1:5000),1));
    pb2(ind) = corr2(running(5001:10000),mean(ac(:,5001:10000),1));
    dark2(ind) = corr2(running(10001:15000),mean(ac(:,10001:15000),1)); 
end
figure,plot(fb),hold on,plot(mean(fb,2),'k','LineWidth',2),plot(fb2,'r','LineWidth',2)
figure,plot(pb),hold on,plot(mean(pb,2),'k','LineWidth',2),plot(pb2,'r','LineWidth',2)
figure,plot(dark),hold on,plot(mean(dark,2),'k','LineWidth',2),plot(dark2,'r','LineWidth',2)

figure,hold on,plot(mean(fb,2),'g','LineWidth',3)
plot(mean(pb,2),'r','LineWidth',2),
plot(mean(dark,2),'k','LineWidth',2)

% temp=proj_meta.rd(layer,1).act(:,nframes(1,:));
% [cc,jj] = sort(max(temp,[],2)');
[cc,jj] = sort(dark(5,:));
for ind=1:timepoints
    figure;
    subplot(4,1,1)
    plot(proj_meta(exp).rd(layer,ind).velP_smoothed(nframes(ind,:)))
    hold on
    plot(proj_meta(exp).rd(layer,ind).velM_smoothed(nframes(ind,:))-0.05,'r')
    axis tight
    axis off
    subplot(4,1,2:4)
    imagesc(proj_meta(exp).rd(layer,ind).act(jj,nframes(ind,:)));
    set(gca,'Clim',[1 4])
end
%%
cell=29;
layer=1;
for ind=1:timepoints
    cell_run(ind,:) = proj_meta(exp).rd(layer,ind).velM_smoothed(nframes(ind,:));
    cell_vis(ind,:) = proj_meta(exp).rd(layer,ind).velP_smoothed(nframes(ind,:));
    cell_ac(ind,:) = proj_meta(exp).rd(layer,ind).act(cell,nframes(ind,:));    
end
figure;
hold on
for ind=1:timepoints
   plot(cell_ac(ind,:)/max(cell_ac(ind,:))+ind*2-0.5,'r');
   plot(cell_run(ind,:)*10+ind*2-1,'b');
   plot(cell_vis(ind,:)*10+ind*2-0.5,'g');
end
title(['layer ' num2str(layer) '  cell ' num2str(cell)])
%%
% avg in all layers
clear cells_ac
tmp=zeros(sum(cell_no),15000);
for ind=1:timepoints
    pop_run(ind,:) = proj_meta(exp).rd(layer,ind).velM_smoothed(nframes(ind,:));
    pop_vis(ind,1:10000) = proj_meta(exp).rd(layer,ind).velP_smoothed(nframes(ind,1:10000));
    pop_vis(ind,10001:15000) = ones(1,5000)*-0.01;
    for jnd=1:4
        tmp(sum(cell_no(1:jnd-1))+1:sum(cell_no(1:jnd)),:) = ...
            proj_meta(exp).rd(jnd,ind).act(:,nframes(ind,:));
    end
    cells_ac(:,:,ind) = tmp;
    pop_ac(ind,:)=mean(tmp,1);
end
figure;
hold on
area([1 1 5000],[0 2*timepoints+1 2*timepoints+1],'FaceColor',[0.85 0.6 0.6],'EdgeColor',[0.85 0.6 0.6])
area([5001 5001 10000],[0 2*timepoints+1 2*timepoints+1],'FaceColor',[0.6 0.8 0.6],'EdgeColor',[0.6 0.8 0.6])
area([10001 10001 15000],[0 2*timepoints+1 2*timepoints+1],'FaceColor',[0.7 0.7 0.7],'EdgeColor',[0.7 0.7 0.7])
for ind=1:timepoints
   plot(pop_ac(ind,:)+ind*2-0.8,'g');
   plot(pop_run(ind,:)*10+ind*2-0.8,'k');
   plot(pop_vis(ind,:)*10+ind*2-0.3,'r');
end
axis tight
set(gca,'XTick',0:5000:15000)
set(gca,'YTick',2:2:2*timepoints)
set(gca,'YTickLabel',1:timepoints)


figure;plot(mean(pop_ac(:,10001:15000),2)','k','Linewidth',3);
hold on;plot(mean(pop_ac(:,5001:10000),2)','g','Linewidth',3);
plot(mean(pop_ac(:,1:5000),2)','r','Linewidth',3);
ylabel('mean cell activity')
set(gca,'XTick',1:1:timepoints)

figure;plot(mean(pop_run(:,10001:15000),2)','k','Linewidth',3);
hold on;plot(mean(pop_run(:,5001:10000),2)','g','Linewidth',3);
plot(mean(pop_run(:,1:5000),2)','r','Linewidth',3);
ylabel('mean running velocity')
set(gca,'XTick',1:1:timepoints)
